<!DOCTYPE html>
<html lang="zh-cn">
<head>
 
    <link type="text/css" rel="stylesheet" href="/bundles/blog-common.css?v=KOZafwuaDasEedEenI5aTy8aXH0epbm6VUJ0v3vsT_Q1"/>
<link id="MainCss" type="text/css" rel="stylesheet" href="/skins/ThinkInside/bundle-ThinkInside.css?v=RRjf6pEarGnbXZ86qxNycPfQivwSKWRa4heYLB15rVE1"/>
<link type="text/css" rel="stylesheet" href="/blog/customcss/428549.css?v=%2fam3bBTkW5NBWhBE%2fD0lcyJv5UM%3d"/>

</head>
<body>
<a name="top"></a>

<div id="page_begin_html"></div><script>load_page_begin_html();</script>

<div id="topics">
	<div class = "post">
		<h1 class = "postTitle">
			<a id="cb_post_title_url" class="postTitle2" href="https://www.cnblogs.com/frankdeng/p/9310262.html">HBase（七）Hbase过滤器</a>
		</h1>
		<div class="clear"></div>
		<div class="postBody">
			<div id="cnblogs_post_body" class="blogpost-body"><h2 id="blogTitle0">一、过滤器（Filter）</h2>
<p>　　基础API中的查询操作在面对大量数据的时候是非常苍白的，这里Hbase提供了高级的查询方法：Filter。Filter可以根据簇、列、版本等更多的条件来对数据进行过滤，基于Hbase本身提供的三维有序（主键有序、列有序、版本有序），这些Filter可以高效的完成查询过滤的任务。带有Filter条件的RPC查询请求会把Filter分发到各个RegionServer，是一个服务器端（Server-side）的过滤器，这样也可以降低网络传输的压力。</p>
<p>　　要完成一个过滤的操作，至少需要两个参数。<strong>一个是抽象的操作符</strong>，Hbase提供了枚举类型的变量来表示这些抽象的操作符：LESS/LESS_OR_EQUAL/EQUAL/NOT_EUQAL等；<strong>另外一个就是具体的比较器（Comparator）</strong>，代表具体的比较逻辑，如果可以提高字节级的比较、字符串级的比较等。有了这两个参数，我们就可以清晰的定义筛选的条件，过滤数据。</p>
<h3 id="blogTitle0">1、<strong>抽象操作符（比较运算符）</strong></h3>
<blockquote>
<p>LESS &lt;</p>
<p>LESS_OR_EQUAL &lt;=</p>
<p>EQUAL =</p>
<p>NOT_EQUAL &lt;&gt;</p>
<p>GREATER_OR_EQUAL &gt;=</p>
<p>GREATER &gt;</p>
<p>NO_OP 排除所有</p>
</blockquote>
<h3 id="blogTitle0">2、<strong>比较器（指定比较机制）</strong></h3>
<blockquote>
<p>BinaryComparator 按字节索引顺序比较指定字节数组，采用 Bytes.compareTo(byte[])</p>
<p>BinaryPrefixComparator 跟前面相同，只是比较左端的数据是否相同</p>
<p>NullComparator 判断给定的是否为空</p>
<p>BitComparator 按位比较</p>
<p>RegexStringComparator 提供一个正则的比较器，仅支持 EQUAL 和非 EQUAL</p>
<p>SubstringComparator 判断提供的子串是否出现在 value 中</p>
</blockquote>
<h2 id="blogTitle0">二、HBase过滤器的分类</h2>
<h2 id="blogTitle0">1、比较过滤器</h2>
<h3>1.行键过滤器 RowFilter</h3>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> HbaseFilterTest {

    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> final String ZK_CONNECT_KEY = <span style="color: #800000;">"</span><span style="color: #800000;">hbase.zookeeper.quorum</span><span style="color: #800000;">"</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> final String ZK_CONNECT_VALUE = <span style="color: #800000;">"</span><span style="color: #800000;">node21:2181,node22:2181,node23:2181</span><span style="color: #800000;">"</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> Connection conn = <span style="color: #0000ff;">null</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> Admin admin = <span style="color: #0000ff;">null</span><span style="color: #000000;">;
    
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args) throws Exception {
        
        Configuration conf </span>=<span style="color: #000000;"> HBaseConfiguration.create();
        conf.</span><span style="color: #0000ff;">set</span><span style="color: #000000;">(ZK_CONNECT_KEY, ZK_CONNECT_VALUE);
        conn </span>=<span style="color: #000000;"> ConnectionFactory.createConnection(conf);
        admin </span>=<span style="color: #000000;"> conn.getAdmin();
        Table table </span>= conn.getTable(TableName.valueOf(<span style="color: #800000;">"</span><span style="color: #800000;">student</span><span style="color: #800000;">"</span><span style="color: #000000;">));       
        Scan scan </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> Scan();        
        <span style="color: #ff0000;">Filter rowFilter </span></span><span style="color: #ff0000;">= new RowFilter(CompareOp.GREATER, new BinaryComparator("95007"</span><span style="color: #000000;"><span style="color: #ff0000;">.getBytes()));
        scan.setFilter(rowFilter);</span>
        ResultScanner resultScanner </span>=<span style="color: #000000;"> table.getScanner(scan);
        </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(Result result : resultScanner) {
            List</span>&lt;Cell&gt; cells =<span style="color: #000000;"> result.listCells();
            </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(Cell cell : cells) {
                System.</span><span style="color: #0000ff;">out</span><span style="color: #000000;">.println(cell);
            }
        }              
    }</span></pre>
</div>
<p>运行结果部分截图</p>
<p><img src="https://images2018.cnblogs.com/blog/1228818/201803/1228818-20180331142700638-33411889.png" alt="" width="586" height="510" /></p>
<h3>2.列簇过滤器 FamilyFilter</h3>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> HbaseFilterTest {

    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> final String ZK_CONNECT_KEY = <span style="color: #800000;">"</span><span style="color: #800000;">hbase.zookeeper.quorum</span><span style="color: #800000;">"</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> final String ZK_CONNECT_VALUE = <span style="color: #800000;">"</span><span style="color: #800000;">node21:2181,node22:2181,node23:2181</span><span style="color: #800000;">"</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> Connection conn = <span style="color: #0000ff;">null</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> Admin admin = <span style="color: #0000ff;">null</span><span style="color: #000000;">;
    
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args) throws Exception {
        
        Configuration conf </span>=<span style="color: #000000;"> HBaseConfiguration.create();
        conf.</span><span style="color: #0000ff;">set</span><span style="color: #000000;">(ZK_CONNECT_KEY, ZK_CONNECT_VALUE);
        conn </span>=<span style="color: #000000;"> ConnectionFactory.createConnection(conf);
        admin </span>=<span style="color: #000000;"> conn.getAdmin();
        Table table </span>= conn.getTable(TableName.valueOf(<span style="color: #800000;">"</span><span style="color: #800000;">student</span><span style="color: #800000;">"</span><span style="color: #000000;">));        
        Scan scan </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> Scan();
       <span style="color: #ff0000;"> Filter familyFilter </span></span><span style="color: #ff0000;">= new FamilyFilter(CompareOp.EQUAL, new BinaryComparator("info"</span><span style="color: #000000;"><span style="color: #ff0000;">.getBytes()));
        scan.setFilter(familyFilter);</span>
        ResultScanner resultScanner </span>=<span style="color: #000000;"> table.getScanner(scan);
        </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(Result result : resultScanner) {
            List</span>&lt;Cell&gt; cells =<span style="color: #000000;"> result.listCells();
            </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(Cell cell : cells) {
                System.</span><span style="color: #0000ff;">out</span><span style="color: #000000;">.println(cell);
            }
        }       
    }   
}</span></pre>
</div>
<p>运行结果部分截图&nbsp;</p>
<p><img src="https://images2018.cnblogs.com/blog/1228818/201803/1228818-20180331142550951-1320138981.png" alt="" /></p>
<h3>3.列过滤器 QualifierFilter</h3>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> HbaseFilterTest {

    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> final String ZK_CONNECT_KEY = <span style="color: #800000;">"</span><span style="color: #800000;">hbase.zookeeper.quorum</span><span style="color: #800000;">"</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> final String ZK_CONNECT_VALUE = <span style="color: #800000;">"</span><span style="color: #800000;">node21:2181,node22:2181,node23:2181</span><span style="color: #800000;">"</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> Connection conn = <span style="color: #0000ff;">null</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> Admin admin = <span style="color: #0000ff;">null</span><span style="color: #000000;">;
    
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args) throws Exception {
        
        Configuration conf </span>=<span style="color: #000000;"> HBaseConfiguration.create();
        conf.</span><span style="color: #0000ff;">set</span><span style="color: #000000;">(ZK_CONNECT_KEY, ZK_CONNECT_VALUE);
        conn </span>=<span style="color: #000000;"> ConnectionFactory.createConnection(conf);
        admin </span>=<span style="color: #000000;"> conn.getAdmin();
        Table table </span>= conn.getTable(TableName.valueOf(<span style="color: #800000;">"</span><span style="color: #800000;">student</span><span style="color: #800000;">"</span><span style="color: #000000;">));
        Scan scan </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> Scan();       
        <span style="color: #ff0000;">Filter qualifierFilter </span></span><span style="color: #ff0000;">= new QualifierFilter(CompareOp.EQUAL, new BinaryComparator("name"</span><span style="color: #000000;"><span style="color: #ff0000;">.getBytes()));
        scan.setFilter(qualifierFilter);</span>
        ResultScanner resultScanner </span>=<span style="color: #000000;"> table.getScanner(scan);
        </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(Result result : resultScanner) {
            List</span>&lt;Cell&gt; cells =<span style="color: #000000;"> result.listCells();
            </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(Cell cell : cells) {
                System.</span><span style="color: #0000ff;">out</span><span style="color: #000000;">.println(cell);
            }
        }              
    }    
}</span></pre>
</div>
<p>运行结果部分截图&nbsp;</p>
<p>&nbsp;<img src="https://images2018.cnblogs.com/blog/1228818/201803/1228818-20180331142440045-1822520821.png" alt="" width="441" height="262" /></p>
<h3>4.值过滤器 ValueFilter</h3>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> HbaseFilterTest {

    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> final String ZK_CONNECT_KEY = <span style="color: #800000;">"</span><span style="color: #800000;">hbase.zookeeper.quorum</span><span style="color: #800000;">"</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> final String ZK_CONNECT_VALUE = <span style="color: #800000;">"</span><span style="color: #800000;">node21:2181,node22:2181,node23:2181</span><span style="color: #800000;">"</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> Connection conn = <span style="color: #0000ff;">null</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> Admin admin = <span style="color: #0000ff;">null</span><span style="color: #000000;">;
    
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args) throws Exception {
        
        Configuration conf </span>=<span style="color: #000000;"> HBaseConfiguration.create();
        conf.</span><span style="color: #0000ff;">set</span><span style="color: #000000;">(ZK_CONNECT_KEY, ZK_CONNECT_VALUE);
        conn </span>=<span style="color: #000000;"> ConnectionFactory.createConnection(conf);
        admin </span>=<span style="color: #000000;"> conn.getAdmin();
        Table table </span>= conn.getTable(TableName.valueOf(<span style="color: #800000;">"</span><span style="color: #800000;">student</span><span style="color: #800000;">"</span><span style="color: #000000;">));       
        Scan scan </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> Scan();      
       <span style="color: #ff0000;"> Filter valueFilter </span></span><span style="color: #ff0000;">= new ValueFilter(CompareOp.EQUAL, new SubstringComparator("男"</span><span style="color: #000000;"><span style="color: #ff0000;">));
        scan.setFilter(valueFilter);</span>
        ResultScanner resultScanner </span>=<span style="color: #000000;"> table.getScanner(scan);
        </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(Result result : resultScanner) {
            List</span>&lt;Cell&gt; cells =<span style="color: #000000;"> result.listCells();
            </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(Cell cell : cells) {
                System.</span><span style="color: #0000ff;">out</span><span style="color: #000000;">.println(cell);
            }
        }              
    }       
}</span></pre>
</div>
<p>运行结果部分截图&nbsp;</p>
<p><img src="https://images2018.cnblogs.com/blog/1228818/201803/1228818-20180331142810385-1010448988.png" alt="" width="470" height="432" /></p>
<h3>5.时间戳过滤器 TimestampsFilter</h3>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> HbaseFilterTest {

    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> final String ZK_CONNECT_KEY = <span style="color: #800000;">"</span><span style="color: #800000;">hbase.zookeeper.quorum</span><span style="color: #800000;">"</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> final String ZK_CONNECT_VALUE = <span style="color: #800000;">"</span><span style="color: #800000;">node21:2181,node22:2181,node23:2181</span><span style="color: #800000;">"</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> Connection conn = <span style="color: #0000ff;">null</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> Admin admin = <span style="color: #0000ff;">null</span><span style="color: #000000;">;
    
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args) throws Exception {
        
        Configuration conf </span>=<span style="color: #000000;"> HBaseConfiguration.create();
        conf.</span><span style="color: #0000ff;">set</span><span style="color: #000000;">(ZK_CONNECT_KEY, ZK_CONNECT_VALUE);
        conn </span>=<span style="color: #000000;"> ConnectionFactory.createConnection(conf);
        admin </span>=<span style="color: #000000;"> conn.getAdmin();
        Table table </span>= conn.getTable(TableName.valueOf(<span style="color: #800000;">"</span><span style="color: #800000;">student</span><span style="color: #800000;">"</span><span style="color: #000000;">));       
        Scan scan </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> Scan();        
       <span style="color: #ff0000;"> List</span></span><span style="color: #ff0000;">&lt;Long&gt; list = new ArrayList&lt;&gt;();
        list.add(1522469029503l);
        TimestampsFilter timestampsFilter = new</span><span style="color: #000000;"><span style="color: #ff0000;"> TimestampsFilter(list);
        scan.setFilter(timestampsFilter);</span>
        ResultScanner resultScanner </span>=<span style="color: #000000;"> table.getScanner(scan);
        </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(Result result : resultScanner) {
            List</span>&lt;Cell&gt; cells =<span style="color: #000000;"> result.listCells();
            </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(Cell cell : cells) {
                System.</span><span style="color: #0000ff;">out</span>.println(Bytes.toString(cell.getRow()) + <span style="color: #800000;">"</span><span style="color: #800000;">\t</span><span style="color: #800000;">"</span> + Bytes.toString(cell.getFamily()) + <span style="color: #800000;">"</span><span style="color: #800000;">\t</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> Bytes.toString(cell.getQualifier())
                </span>+ <span style="color: #800000;">"</span><span style="color: #800000;">\t</span><span style="color: #800000;">"</span> + Bytes.toString(cell.getValue()) + <span style="color: #800000;">"</span><span style="color: #800000;">\t</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> cell.getTimestamp());
            }
        }                
    }       
}</span></pre>
</div>
<p>运行结果部分截图&nbsp;</p>
<p><img src="https://images2018.cnblogs.com/blog/1228818/201803/1228818-20180331143422193-1859430510.png" alt="" width="424" height="319" /></p>
<h2 id="blogTitle0">2、专用过滤器</h2>
<h3>1.单列值过滤器 SingleColumnValueFilter</h3>
<p>会返回满足条件的整行</p>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> HbaseFilterTest2 {

    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> final String ZK_CONNECT_KEY = <span style="color: #800000;">"</span><span style="color: #800000;">hbase.zookeeper.quorum</span><span style="color: #800000;">"</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> final String ZK_CONNECT_VALUE = <span style="color: #800000;">"</span><span style="color: #800000;">node21:2181,node22:2181,node23:2181</span><span style="color: #800000;">"</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> Connection conn = <span style="color: #0000ff;">null</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> Admin admin = <span style="color: #0000ff;">null</span><span style="color: #000000;">;
    
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args) throws Exception {
        
        Configuration conf </span>=<span style="color: #000000;"> HBaseConfiguration.create();
        conf.</span><span style="color: #0000ff;">set</span><span style="color: #000000;">(ZK_CONNECT_KEY, ZK_CONNECT_VALUE);
        conn </span>=<span style="color: #000000;"> ConnectionFactory.createConnection(conf);
        admin </span>=<span style="color: #000000;"> conn.getAdmin();
        Table table </span>= conn.getTable(TableName.valueOf(<span style="color: #800000;">"</span><span style="color: #800000;">student</span><span style="color: #800000;">"</span><span style="color: #000000;">));        
        Scan scan </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> Scan();       
       <span style="color: #ff0000;"> SingleColumnValueFilter singleColumnValueFilter </span></span><span style="color: #ff0000;">= new SingleColumnValueFilter(
                "info".getBytes(), 
                "name".getBytes(), 
                CompareOp.EQUAL, 
                new SubstringComparator("刘晨"));
        //如果不设置为 true，则那些不包含指定 column 的行也会返回
        singleColumnValueFilter.setFilterIfMissing(true</span><span style="color: #000000;"><span style="color: #ff0000;">);        
        scan.setFilter(singleColumnValueFilter);</span>
        ResultScanner resultScanner </span>=<span style="color: #000000;"> table.getScanner(scan);
        </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(Result result : resultScanner) {
            List</span>&lt;Cell&gt; cells =<span style="color: #000000;"> result.listCells();
            </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(Cell cell : cells) {
                System.</span><span style="color: #0000ff;">out</span>.println(Bytes.toString(cell.getRow()) + <span style="color: #800000;">"</span><span style="color: #800000;">\t</span><span style="color: #800000;">"</span> + Bytes.toString(cell.getFamily()) + <span style="color: #800000;">"</span><span style="color: #800000;">\t</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> Bytes.toString(cell.getQualifier())
                </span>+ <span style="color: #800000;">"</span><span style="color: #800000;">\t</span><span style="color: #800000;">"</span> + Bytes.toString(cell.getValue()) + <span style="color: #800000;">"</span><span style="color: #800000;">\t</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> cell.getTimestamp());
            }
        }       
    }    
}</span></pre>
</div>
<p>运行结果部分截图</p>
<p><img src="https://images2018.cnblogs.com/blog/1228818/201803/1228818-20180331145327073-421109265.png" alt="" /></p>
<h3>2.单列值排除器 SingleColumnValueExcludeFilter&nbsp;</h3>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> HbaseFilterTest2 {

    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> final String ZK_CONNECT_KEY = <span style="color: #800000;">"</span><span style="color: #800000;">hbase.zookeeper.quorum</span><span style="color: #800000;">"</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> final String ZK_CONNECT_VALUE = <span style="color: #800000;">"</span><span style="color: #800000;">node21:2181,node22:2181,node23:2181</span><span style="color: #800000;">"</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> Connection conn = <span style="color: #0000ff;">null</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> Admin admin = <span style="color: #0000ff;">null</span><span style="color: #000000;">;
    
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args) throws Exception {
        
        Configuration conf </span>=<span style="color: #000000;"> HBaseConfiguration.create();
        conf.</span><span style="color: #0000ff;">set</span><span style="color: #000000;">(ZK_CONNECT_KEY, ZK_CONNECT_VALUE);
        conn </span>=<span style="color: #000000;"> ConnectionFactory.createConnection(conf);
        admin </span>=<span style="color: #000000;"> conn.getAdmin();
        Table table </span>= conn.getTable(TableName.valueOf(<span style="color: #800000;">"</span><span style="color: #800000;">student</span><span style="color: #800000;">"</span><span style="color: #000000;">));       
        Scan scan </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> Scan();     
        <span style="color: #ff0000;">SingleColumnValueExcludeFilter singleColumnValueExcludeFilter </span></span><span style="color: #ff0000;">= new SingleColumnValueExcludeFilter(
                "info".getBytes(), 
                "name".getBytes(), 
                CompareOp.EQUAL, 
                new SubstringComparator("刘晨"));
        singleColumnValueExcludeFilter.setFilterIfMissing(true</span><span style="color: #000000;"><span style="color: #ff0000;">);       
        scan.setFilter(singleColumnValueExcludeFilter);</span>
        ResultScanner resultScanner </span>=<span style="color: #000000;"> table.getScanner(scan);
        </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(Result result : resultScanner) {
            List</span>&lt;Cell&gt; cells =<span style="color: #000000;"> result.listCells();
            </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(Cell cell : cells) {
                System.</span><span style="color: #0000ff;">out</span>.println(Bytes.toString(cell.getRow()) + <span style="color: #800000;">"</span><span style="color: #800000;">\t</span><span style="color: #800000;">"</span> + Bytes.toString(cell.getFamily()) + <span style="color: #800000;">"</span><span style="color: #800000;">\t</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> Bytes.toString(cell.getQualifier())
                </span>+ <span style="color: #800000;">"</span><span style="color: #800000;">\t</span><span style="color: #800000;">"</span> + Bytes.toString(cell.getValue()) + <span style="color: #800000;">"</span><span style="color: #800000;">\t</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> cell.getTimestamp());
            }
        }    
    }      
}</span></pre>
</div>
<p>运行结果部分截图&nbsp;</p>
<p>&nbsp;<img src="https://images2018.cnblogs.com/blog/1228818/201803/1228818-20180331145530417-602264605.png" alt="" /></p>
<h3>3.前缀过滤器 PrefixFilter----针对行键</h3>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> HbaseFilterTest2 {

    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> final String ZK_CONNECT_KEY = <span style="color: #800000;">"</span><span style="color: #800000;">hbase.zookeeper.quorum</span><span style="color: #800000;">"</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> final String ZK_CONNECT_VALUE = <span style="color: #800000;">"</span><span style="color: #800000;">node21:2181,node22:2181,node23:2181</span><span style="color: #800000;">"</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> Connection conn = <span style="color: #0000ff;">null</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> Admin admin = <span style="color: #0000ff;">null</span><span style="color: #000000;">;
    
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args) throws Exception {
        
        Configuration conf </span>=<span style="color: #000000;"> HBaseConfiguration.create();
        conf.</span><span style="color: #0000ff;">set</span><span style="color: #000000;">(ZK_CONNECT_KEY, ZK_CONNECT_VALUE);
        conn </span>=<span style="color: #000000;"> ConnectionFactory.createConnection(conf);
        admin </span>=<span style="color: #000000;"> conn.getAdmin();
        Table table </span>= conn.getTable(TableName.valueOf(<span style="color: #800000;">"</span><span style="color: #800000;">student</span><span style="color: #800000;">"</span><span style="color: #000000;">));        
        Scan scan </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> Scan();        
     <span style="color: #ff0000;">   PrefixFilter prefixFilter </span></span><span style="color: #ff0000;">= new PrefixFilter("9501"</span><span style="color: #000000;"><span style="color: #ff0000;">.getBytes());        
        scan.setFilter(prefixFilter);</span>
        ResultScanner resultScanner </span>=<span style="color: #000000;"> table.getScanner(scan);
        </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(Result result : resultScanner) {
            List</span>&lt;Cell&gt; cells =<span style="color: #000000;"> result.listCells();
            </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(Cell cell : cells) {
                System.</span><span style="color: #0000ff;">out</span>.println(Bytes.toString(cell.getRow()) + <span style="color: #800000;">"</span><span style="color: #800000;">\t</span><span style="color: #800000;">"</span> + Bytes.toString(cell.getFamily()) + <span style="color: #800000;">"</span><span style="color: #800000;">\t</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> Bytes.toString(cell.getQualifier())
                </span>+ <span style="color: #800000;">"</span><span style="color: #800000;">\t</span><span style="color: #800000;">"</span> + Bytes.toString(cell.getValue()) + <span style="color: #800000;">"</span><span style="color: #800000;">\t</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> cell.getTimestamp());
            }
        }        
    }   
}</span></pre>
</div>
<p>运行结果部分截图&nbsp;</p>
<p><img src="https://images2018.cnblogs.com/blog/1228818/201803/1228818-20180331150009860-2063523179.png" alt="" width="554" height="548" /></p>
<h3>4.列前缀过滤器 ColumnPrefixFilter</h3>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> HbaseFilterTest2 {

    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> final String ZK_CONNECT_KEY = <span style="color: #800000;">"</span><span style="color: #800000;">hbase.zookeeper.quorum</span><span style="color: #800000;">"</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> final String ZK_CONNECT_VALUE = <span style="color: #800000;">"</span><span style="color: #800000;">node21:2181,node22:2181,node23:2181</span><span style="color: #800000;">"</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> Connection conn = <span style="color: #0000ff;">null</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> Admin admin = <span style="color: #0000ff;">null</span><span style="color: #000000;">;
    
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(String[] args) throws Exception {
        
        Configuration conf </span>=<span style="color: #000000;"> HBaseConfiguration.create();
        conf.</span><span style="color: #0000ff;">set</span><span style="color: #000000;">(ZK_CONNECT_KEY, ZK_CONNECT_VALUE);
        conn </span>=<span style="color: #000000;"> ConnectionFactory.createConnection(conf);
        admin </span>=<span style="color: #000000;"> conn.getAdmin();
        Table table </span>= conn.getTable(TableName.valueOf(<span style="color: #800000;">"</span><span style="color: #800000;">student</span><span style="color: #800000;">"</span><span style="color: #000000;">));       
        Scan scan </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> Scan();        
      <span style="color: #ff0000;">  ColumnPrefixFilter columnPrefixFilter </span></span><span style="color: #ff0000;">= new ColumnPrefixFilter("name"</span><span style="color: #000000;"><span style="color: #ff0000;">.getBytes());     
        scan.setFilter(columnPrefixFilter);</span>
        ResultScanner resultScanner </span>=<span style="color: #000000;"> table.getScanner(scan);
        </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(Result result : resultScanner) {
            List</span>&lt;Cell&gt; cells =<span style="color: #000000;"> result.listCells();
            </span><span style="color: #0000ff;">for</span><span style="color: #000000;">(Cell cell : cells) {
                System.</span><span style="color: #0000ff;">out</span>.println(Bytes.toString(cell.getRow()) + <span style="color: #800000;">"</span><span style="color: #800000;">\t</span><span style="color: #800000;">"</span> + Bytes.toString(cell.getFamily()) + <span style="color: #800000;">"</span><span style="color: #800000;">\t</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> Bytes.toString(cell.getQualifier())
                </span>+ <span style="color: #800000;">"</span><span style="color: #800000;">\t</span><span style="color: #800000;">"</span> + Bytes.toString(cell.getValue()) + <span style="color: #800000;">"</span><span style="color: #800000;">\t</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> cell.getTimestamp());
            }
        }   
    }   
}</span></pre>
</div>
<p>&nbsp;运行结果部分截图</p>
<p><img src="https://images2018.cnblogs.com/blog/1228818/201803/1228818-20180331150519676-692732493.png" alt="" /></p>
<h3>5.分页过滤器 PageFilter</h3></div>

</body>
</html>
